Java Static Class
In java, it is possible to create a class inside an other class. We call it as Nested Class
.
The class which has the nested class is known as Outer Class
. To access the nested class we need to use the outer class instance. As a result, an instance of an inner class can access all the members of its outer class without referencing an instance of the outer class.
It is impossible to construct an instance of an inner class without an instance of the outer class first.
Example program for static class
class Animal {
// normal inner class
class Reptile {
public void display() {
System.out.println("Reptile Category.");
}
}
// static inner class
static class Mammal {
public void display() {
System.out.println("Mammal Category.");
}
}
}
class Main {
public static void main(String[] args) {
// outer class object creation
Animal animal = new Animal();
// non-static class object creation
Animal.Reptile reptile = animal.new Reptile();
reptile.display();
// static nested class object creation
Animal.Mammal mammal = new Animal.Mammal();
mammal.display();
}
}
Output
Reptile Category.
Mammal Category.
In the above program, we have a class Animal
with one static nested class Mammal
and one non-static nested class, Reptile
.
Both the nested class will have a method display()
, to display its category.
Now, in the Main
class, we create an object for the Animal
class. With the Animal
class object we will create object for non-static nested class with the following code
Animal.Reptile reptile = animal.new Reptile();
reptile.display();
Now, with the reptile
object we can call the display()
method.
Similarly, to create object for the nested static class Mammal
, we use the following code.
Animal.Mammal mammal = new Animal.Mammal();
mammal.display();
The static nested classes can access only the static fields and static methods of the outer class. If we try to access the non-static members of the outer class then it will throw an error.
Example to access non-static members
class Animal {
// normal inner class
class Reptile {
public void display() {
System.out.println("Reptile Category.");
}
}
// static inner class
static class Mammal {
public void display() {
System.out.println("Mammal Category.");
}
}
// non-static method
void eat(){
System.out.println("I will eat food.");
}
}
class Main {
public static void main(String[] args) {
// outer class object creation
Animal animal = new Animal();
// non-static class object creation
Animal.Reptile reptile = animal.new Reptile();
reptile.display();
// static nested class object creation
Animal.Mammal mammal = new Animal.Mammal();
mammal.display();
mammal.eat(); // Throw an error.
}
}
In the above example, when we try to access non-static method eat()
of Animal
class with the mammal
object which will throw an error as below.
Main.java:34: error: cannot find symbol
mammal.eat();
^
symbol: method eat()
location: variable mammal of type Mammal
1 error
We can modify the above program to access the static method of the outer class from the inner class.
class Animal {
// normal inner class
class Reptile {
public void display() {
System.out.println("Reptile Category.");
}
}
// static inner class
static class Mammal {
public void display() {
System.out.println("Mammal Category.");
}
public void displayEatBehavior(){
eat();
}
}
static void eat(){
System.out.println("I will eat food.");
}
}
class Main {
public static void main(String[] args) {
// outer class object creation
Animal animal = new Animal();
// non-static class object creation
Animal.Reptile reptile = animal.new Reptile();
reptile.display();
// static nested class object creation
Animal.Mammal mammal = new Animal.Mammal();
mammal.display();
mammal.displayEatBehavior();
}
}
In the above example, we have created a static
eat()
method, for the Animal
class and call the eat()
method inside the Mammal
class.